import SSE from "sse.js";

const connect = R.curryN(_connect, 4);

function _connect(url, headers, onMessage, onError) {
  this.sse = new SSE(url, { headers });
  this.sse.addEventListener("message", onMessage);
  this.sse.addEventListener("error", onError);

  return sse;
}
_connect.sse = null;
_connect.prototype.close = function () {
  this.sse.close();
};

function createSSE(onMessage, onError = R.identity) {
  return connect(onMessage, onError);
}
export default createSSE;
